import {Injectable} from '@angular/core';
import {AlertController, LoadingController, ModalController, Platform, ToastController} from '@ionic/angular';


import { Device } from '@awesome-cordova-plugins/device/ngx';
import { Network } from '@awesome-cordova-plugins/network/ngx';


import {StorageService} from './storage.service';

/**
 * @desc:自定义native相关能力Service，不太好理解的这里引入Device服务，其他使用NativeService的组件，
 *      如果Native没有提供Device API的封装那么他又要直接引入Device，
 *      因此这个所谓的NativeService是否必要......
 */
@Injectable({
    providedIn: 'root'
})
export class NativeService{
    constructor(
        private platform:Platform,
        private alertCtrl: AlertController,
        private device:Device,
        private network:Network,
        private storage: StorageService
    ){

    }
    /**
     * 是否真机环境
     */
    isMobile(): boolean {
        return (this.platform.is('mobile') && !this.platform.is('mobileweb')) || (this.platform.is('cordova'));
    }
     /**
     * 是否android真机环境
     */
      isAndroid(): boolean {
        return this.isMobile() && this.platform.is('android');
    }

    /**
     * 是否ios真机环境
     */
    isIos(): boolean {
        return this.isMobile() && (this.platform.is('ios') || this.platform.is('ipad') || this.platform.is('iphone'));
    }
    /**
     * 获取网络类型 如`unknown`, `ethernet`, `wifi`, `2g`, `3g`, `4g`, `cellular`, `none`
     */
    getNetworkType(): string {
        if (!this.isMobile()) {
            return 'wifi';
        }
        return this.network.type;
    }
    /**
     * 判断是否有网络
     */
    isConnecting():boolean{
        return this.getNetworkType() !== 'none';
    }
    /**
     * 确认回调
     */    
    okHandler() {
        console.log('ok button clicked');
    }
    /**
     * confirm框取消回到
     */
    cancelHandler() {
        console.log('cancel button clicked');
    }
    /**
     * 
     * @param subHeader 显示一个alert框，包含标题、alert内容、右下角确认按钮
     * @param okButtonText 
     * @param okHandler 
     * @param header 
     * @returns 
     */
    async showAlert(subHeader = '',
        okButtonText = '确定',
        okHandler = this.okHandler,
        header = '温馨提示') {
        let alert = await this.alertCtrl.getTop();
        if (alert) {
            await alert.dismiss();
        } 
        alert = await this.alertCtrl.create({
            header,
            subHeader,
            buttons: [{
                text: okButtonText,
                handler: okHandler
            }],
            id: 'lark-alert'
        });
        return await alert.present();
    }
    async showConfirm(subHeader = '',
                      okButtonText = '确定',
                      okHandler = this.okHandler,
                      cancelButtonText = '取消',
                      cancelHandler = this.cancelHandler,
                      backdropDismiss = true,
                      header = '温馨提示') {
        let confirm = await this.alertCtrl.getTop();
        this.storage.write('alert', 'yes');
        if (confirm) {
            await confirm.dismiss();
        }
        confirm = await this.alertCtrl.create({
            header,
            subHeader,
            backdropDismiss,
            buttons: [

                {
                    text: cancelButtonText,
                    handler: cancelHandler
                },
                {
                    text: okButtonText,
                    handler: okHandler
                },
            ],
            id: 'lark-alert'
        });
        return await confirm.present();
    }
}